import java.util.Arrays;

/**
 * Created with Intellij IDEA.
 * Description
 * User: wen
 * Date:2022-03-23
 * TIME:11:29
 */
class Person{//类相当于一个模板
    //字段-属性-成员变量
    //成员变量要在类的内部，方法的外部
    //成员变量分为两种
    // 1.普通成员变量2.静态成员变量
    public String name;//不建议赋值
    public int age;
    //成员方法->行为
    public void eat(){
        System.out.println(name+"正在吃饭");
    }
    public void sleep(){
        System.out.println(name+"正在睡觉");
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Person person = new Person();
        person.name = "abc";
        person.age = 10;
        //普通的成员变量的访问需要通过对象的引用来访问的
        //通过new Person()对象的引用person访问name
        System.out.println(person.name);
        System.out.println(person.age);
        //调用方法
        person.eat();//通过对象的引用来调用方法
        person.sleep();
    }

    public static void main19(String[] args) {
        //Person person = null;
        Person person1 = new Person();//实例化对象
        Person person2 = new Person();
        Person person3 = new Person();
        Person person4 = new Person();
        Person person5 = new Person();

    }



    public static void main18(String[] args) {
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = i+1;
        }
        System.out.println(Arrays.toString(array));
    }
    //
    public static void main17(String[] args) {
        int[][] array2 = new int[2][];
    }
    //
    public static void main16(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        System.out.println(Arrays.deepToString(array));
    }
    //
    public static void main15(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        for (int[] ret:array) {
            for (int x:ret) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
    }
    //
    public static void main14(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static void main13(String[] args) {
        //二维数组的三种定义方式
        int[][] array = {{1,2,3},{4,5,6}};//int[][]中的[]不能加数字
        int[][] array2 = new int[][]{{1,2,3},{4,5,6}};//new int[][]中的[]不能加数字
        int[][] array3 = new int[2][3];
    }
    //
    public static void main12(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int[] ret = array.clone();
        System.out.println(Arrays.toString(ret));
    }
    //---------------------------------------------------
    public static void main11(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9};
        int[] ret = Arrays.copyOfRange(array, 1, 3);//[1,3)
        System.out.println(Arrays.toString(ret));
    }
    //-------------------------------------------------------------
        public static void main10(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int[] copy = new int[array.length];
        System.arraycopy(array, 0, copy, 0, array.length);
        //System.arraycopy(源数组, 从源数组这个位置开始拷贝, copy, 0, 拷贝的长度);
        System.out.println(Arrays.toString(copy));
    }
    //--------------------------------------------------------------------
    //copyOf实现数组拷贝
    public static void main9(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int[] ret = Arrays.copyOf(array, array.length);
        //Arrays.copyOf(数组名，想要拷过去的数组长度)
        //int[] ret = Arrays.copyOf(array, array.length*2);
        //[1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0]
        System.out.println(Arrays.toString(ret));
    }
    //------------------------------------------------------------------
    //for循环实现数组拷贝
    public static int[] copyArray(int[] array){
        int[] copy = new int[array.length];
        int i = 0;
        for(i = 0; i < array.length; i++){
            copy[i] = array[i];
        }
        return copy;
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int[] ret = copyArray(array);
        System.out.println(Arrays.toString(ret));
    }
    //-----------------------------------------------------------------------
    //偶数放在奇数前面
    public static void func(int[] array){
        int i = 0;
        int j = array.length - 1;
        while(i < j){
            //为什么要用while不用if，因为i可能要走很多步才能碰到偶数
            while(i < j && array[i] % 2 == 0) {
                //这是一个单独的循环，不加i<j的话，数组如果全是奇数可能会越界
                i++;
            }//i遇到了奇数
            while(i < j && array[j] % 2 != 0) {
                //这是一个单独的循环，不加i<j的话，数组如果全是偶数j一直--就会越界
                j--;
            }//j遇到了偶数
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }
    }
    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5,6};
        func(array);
        System.out.println(Arrays.toString(array));
    }
    //-------------------------------------------------------------------
    public static void reverse(int[] array){
        int i = 0;
        int j = array.length - 1;
        while (i < j){
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
            i++;
            j--;
        }
    }
    public static void main6(String[] args) {
        int[] array = {11,2,3,14,15};
        reverse(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main5(String[] args) {
        int[] array = new int[10];
        Arrays.fill(array,2,6,1999);//[2,6)
        System.out.println(Arrays.toString(array));
        //[0, 0, 1999, 1999, 1999, 1999, 0, 0, 0, 0]
    }
    public static void main4(String[] args) {
        int[] array = new int[10];
        Arrays.fill(array,99);
        System.out.println(Arrays.toString(array));
        //[99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
    }
    public static void main3(String[] args) {
        int[] array = {11,2,3,14,15};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
    //---------------------------------------------------
    public static void bubble_sort(int[] array){
        //boolean flag = false;//错误
        //i表示趟数
        for(int i = 0; i < array.length; i++){
            boolean flag = false;
            //每交换一趟都要重置false，不每一趟重置的话无论有没有交换flag的值一直是true
            for(int j = 0; j < array.length - 1 - i; j++){
                if(array[j] > array[j+1])
                {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flag = true;
                }
            }
            //如果一趟比较完后数字没有交换，那么说明顺序已经排完
            //
            if(flag == false){
                return;
            }
        }
    }
    public static void main2(String[] args) {
        int[] array = {11,12,3,14,15};
        bubble_sort(array);
        System.out.println(Arrays.toString(array));

    }

    //-------------------------------------------------------
    //判断一个数组是否升序
    public static boolean isup(int[] array ){
        for(int i = 0; i < array.length - 1; i++){
            if(array[i] > array[i+1]){
                return false;
            }
        }
        return true;
    }
    public static void main1(String[] args) {
        int[] array = {11,12,13,14,15};
        boolean flag = isup(array);
        System.out.println(flag);
    }
}
